<!DOCTYPE html>
<html>
<head>
		<title>设置个人资料</title>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
		<link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
		<script src="/js/jquery/jquery.min.js"></script>
		<script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="/css/font/css/font-awesome.min.css" />
		<link rel="stylesheet" href="/css/reset.css" />
		<link rel="stylesheet" href="/css/myInfo.css" />
		<script src="/js/plugins/jrender/jrender.min.js"></script>
		<link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
		<script src="/js/plugins/dialog2/dialog.min.js"></script>
		<script src="/js/plugins/form/jquery.form.js"></script>
<script>
    $(function () {
        //取出来的时候需要把json字符串转回json对象串
        var user = JSON.parse(sessionStorage.getItem("user"));
        console.log(user);

        //渲染用户数据
        $(".info").renderValues(user);
        //背景图片需要手动回显(添加样式 背景)
		$(".myBg").css("background-image","url("+user.coverImgUrl+")");
		//设置表单的提交地址
		var url = "/users/"+user.id;
		$("#infoForm").attr("action",url);
		//给保存按钮绑定点击事件
		$("#saveBtn").click(function () {
			//提交表单到用户信息修改接口
            $("#infoForm").ajaxSubmit(function (data) {
                //更新页面的登录对象信息
                sessionStorage.setItem("user",JSON.stringify(data.result));
                //修改成功,跳转
                window.location.href = "/mine/profiles.html";
            })
        });
		var flag = true;//默认上传头像
		//更换头像绑定点击事件
		$("#headBtn").click(function () {
			//触发文件上传
			$("#fileinputBtn").click();
            flag = true;
        });
		//设置背景绑定点击事件
		$("#coverBtn").click(function () {
			//触发文件上传
			$("#fileinputBtn").click();
            flag = false;
        });
        //给文件上传的值改变绑定事件提交表单
        $("#fileinputBtn").change(function () {
            //如果有值才进行提交
            if ($(this).val()) {
                //图片上传提交表单
                $("#fileForm").ajaxSubmit(function (data) {
                    console.log(data);
                    console.log(data.url);
                    if (data.status==1) {
                        if(flag){
                            //把图片回显到头像中
                            //回显表单中的隐藏域
                            $("#headImg").attr("src",data.url);
                            $("#infoForm input[name='headImgUrl']").val(data.url);
                        }else{
                            //把图片回显到背景中
                            $(".myBg").css("background-image","url("+data.url+")");
                            //回显表单中的隐藏域
                            $("#infoForm input[name='coverImgUrl']").val(data.url);
                        }
                    }
                });
            }
        });
    });
</script>
	</head>

<body>
<!--文件上传表单-->
<form id="fileForm" action="/images" method="post" enctype="multipart/form-data">
	<input id="fileinputBtn" type="file" name="file" style="display: none" >
</form>
<div class="search-head">
	<div class="row nav-search">
		<div class="col">
					<a href="javascript:window.history.go(-1);">
						<span>取消</span>
					</a>
				</div>
				<div class="col">
					<span>设置个人资料</span>
				</div>
				<div class="col" >
					<a role="button" id="saveBtn" >
						<span>保存</span>
					</a>
				</div>
			</div>
		</div>
		<div class="container info">
			<div class="myBg">
				<img id="headImg" render-src="headImgUrl" class="rounded-circle"/>
				<div class="editHead" id="headBtn" ><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
				<button  class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
			</div>

			<form id="infoForm" method="post">
					<!--以put方式提交表单-->
					<input type="hidden" name="_method" value="put">
				<!--图片是文件上传,不能和普通表单一起提交-->
					<input type="hidden" name="headImgUrl" render-value="headImgUrl">
					<input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
					<input type="hidden" name="email" render-value="email">

					<label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
					<label>性别</label><select class="form-control" name="gender" render-value="gender">
							<option value="-1">保密</option>
							<option value="1">男</option>
							<option value="0">女</option>
						</select>
					<label>地区</label><input type="text" name="place"  class="form-control" render-value="place">
					<label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
			</form>
		</div>
		
		
		
	</body>
</html>
